<!DOCTYPE html>
<html lang=en>
<head>
    <!-- so meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="HandheldFriendly" content="True">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
    <meta name="description" content="最近看博客园中 焰尾迭 的两篇关于”Newtonsoft.Json高级用法”的文章受到了很多人的评论，一度登入到头条推荐。 今天我就不再重复焰尾迭博文中的一些提过的Newtonsoft.Json的高级用法。大家如果想知道直接去看。 Newtonsoft.Json高级用法再谈Newtonsoft.Json高级用法我主要说焰尾迭 没有提到的用法——枚举中文转义 枚举值序列化问题（摘自焰尾迭 段落）p">
<meta property="og:type" content="article">
<meta property="og:title" content="Newtonsoft.Json高级用法之枚举中文转义Newtonsoft.Json高级用法之枚举中文转义">
<meta property="og:url" content="http://yoursite.com/2019/01/06/Newtonsoft.Json%E9%AB%98%E7%BA%A7%E7%94%A8%E6%B3%95%E4%B9%8B%E6%9E%9A%E4%B8%BE%E4%B8%AD%E6%96%87%E8%BD%AC%E4%B9%89Newtonsoft.Json%E9%AB%98%E7%BA%A7%E7%94%A8%E6%B3%95%E4%B9%8B%E6%9E%9A%E4%B8%BE%E4%B8%AD%E6%96%87%E8%BD%AC%E4%B9%89/index.html">
<meta property="og:site_name" content="TmoonSite">
<meta property="og:description" content="最近看博客园中 焰尾迭 的两篇关于”Newtonsoft.Json高级用法”的文章受到了很多人的评论，一度登入到头条推荐。 今天我就不再重复焰尾迭博文中的一些提过的Newtonsoft.Json的高级用法。大家如果想知道直接去看。 Newtonsoft.Json高级用法再谈Newtonsoft.Json高级用法我主要说焰尾迭 没有提到的用法——枚举中文转义 枚举值序列化问题（摘自焰尾迭 段落）p">
<meta property="og:locale" content="en_US">
<meta property="og:image" content="https://images2015.cnblogs.com/blog/336693/201509/336693-20150910171743028-388882267.png">
<meta property="og:image" content="https://common.cnblogs.com/images/copycode.gif">
<meta property="og:image" content="https://common.cnblogs.com/images/copycode.gif">
<meta property="og:image" content="https://common.cnblogs.com/images/copycode.gif">
<meta property="og:image" content="https://common.cnblogs.com/images/copycode.gif">
<meta property="og:image" content="https://images2015.cnblogs.com/blog/336693/201509/336693-20150910172109044-1714459886.png">
<meta property="og:image" content="https://common.cnblogs.com/images/copycode.gif">
<meta property="og:image" content="https://common.cnblogs.com/images/copycode.gif">
<meta property="og:image" content="https://common.cnblogs.com/images/copycode.gif">
<meta property="og:image" content="https://common.cnblogs.com/images/copycode.gif">
<meta property="og:image" content="https://common.cnblogs.com/images/copycode.gif">
<meta property="og:image" content="https://common.cnblogs.com/images/copycode.gif">
<meta property="og:image" content="">
<meta property="og:image" content="">
<meta property="article:published_time" content="2019-01-05T23:13:35.000Z">
<meta property="article:modified_time" content="2020-01-07T10:48:14.747Z">
<meta property="article:author" content="Tmoonlight">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://images2015.cnblogs.com/blog/336693/201509/336693-20150910171743028-388882267.png">
    
    
        
          
              <link rel="shortcut icon" href="/images/favicon.ico">
          
        
        
          
            <link rel="icon" type="image/png" href="/images/logo2.gif" sizes="192x192">
          
        
        
          
            <link rel="apple-touch-icon" sizes="180x180" href="/images/logo2.gif">
          
        
    
    <!-- title -->
    <title>Newtonsoft.Json高级用法之枚举中文转义Newtonsoft.Json高级用法之枚举中文转义</title>
    <!-- styles -->
    
<link rel="stylesheet" href="/css/style.css">

    <!-- persian styles -->
    
      
<link rel="stylesheet" href="/css/rtl.css">

    
    <!-- rss -->
    
    
<meta name="generator" content="Hexo 4.2.0"></head>

<body class="max-width mx-auto px3 ltr">
    
      <div id="header-post">
  <a id="menu-icon" href="#"><i class="fas fa-bars fa-lg"></i></a>
  <a id="menu-icon-tablet" href="#"><i class="fas fa-bars fa-lg"></i></a>
  <a id="top-icon-tablet" href="#" onclick="$('html, body').animate({ scrollTop: 0 }, 'fast');" style="display:none;"><i class="fas fa-chevron-up fa-lg"></i></a>
  <span id="menu">
    <span id="nav">
      <ul>
         
          <li><a href="/">Home</a></li>
         
          <li><a href="/about/">About</a></li>
         
          <li><a href="/archives/">Writing</a></li>
         
          <li><a href="/projects_url">Projects</a></li>
        
      </ul>
    </span>
    <br/>
    <span id="actions">
      <ul>
        
        <li><a class="icon" href="/2019/01/16/%E9%A6%99%E6%B8%AF%E8%B4%A6%E6%88%B7/"><i class="fas fa-chevron-left" aria-hidden="true" onmouseover="$('#i-prev').toggle();" onmouseout="$('#i-prev').toggle();"></i></a></li>
        
        
        <li><a class="icon" href="/2018/12/28/CoreCLR%E6%BA%90%E7%A0%81%E6%8E%A2%E7%B4%A2(%E4%B8%83)JIT%E7%9A%84%E5%B7%A5%E4%BD%9C%E5%8E%9F%E7%90%86(%E5%85%A5%E9%97%A8%E7%AF%87)/"><i class="fas fa-chevron-right" aria-hidden="true" onmouseover="$('#i-next').toggle();" onmouseout="$('#i-next').toggle();"></i></a></li>
        
        <li><a class="icon" href="#" onclick="$('html, body').animate({ scrollTop: 0 }, 'fast');"><i class="fas fa-chevron-up" aria-hidden="true" onmouseover="$('#i-top').toggle();" onmouseout="$('#i-top').toggle();"></i></a></li>
        <li><a class="icon" href="#"><i class="fas fa-share-alt" aria-hidden="true" onmouseover="$('#i-share').toggle();" onmouseout="$('#i-share').toggle();" onclick="$('#share').toggle();return false;"></i></a></li>
      </ul>
      <span id="i-prev" class="info" style="display:none;">Previous post</span>
      <span id="i-next" class="info" style="display:none;">Next post</span>
      <span id="i-top" class="info" style="display:none;">Back to top</span>
      <span id="i-share" class="info" style="display:none;">Share post</span>
    </span>
    <br/>
    <div id="share" style="display: none">
      <ul>
  <li><a class="icon" href="http://www.facebook.com/sharer.php?u=http://yoursite.com/2019/01/06/Newtonsoft.Json%E9%AB%98%E7%BA%A7%E7%94%A8%E6%B3%95%E4%B9%8B%E6%9E%9A%E4%B8%BE%E4%B8%AD%E6%96%87%E8%BD%AC%E4%B9%89Newtonsoft.Json%E9%AB%98%E7%BA%A7%E7%94%A8%E6%B3%95%E4%B9%8B%E6%9E%9A%E4%B8%BE%E4%B8%AD%E6%96%87%E8%BD%AC%E4%B9%89/" target="_blank" rel="noopener"><i class="fab fa-facebook " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://twitter.com/share?url=http://yoursite.com/2019/01/06/Newtonsoft.Json%E9%AB%98%E7%BA%A7%E7%94%A8%E6%B3%95%E4%B9%8B%E6%9E%9A%E4%B8%BE%E4%B8%AD%E6%96%87%E8%BD%AC%E4%B9%89Newtonsoft.Json%E9%AB%98%E7%BA%A7%E7%94%A8%E6%B3%95%E4%B9%8B%E6%9E%9A%E4%B8%BE%E4%B8%AD%E6%96%87%E8%BD%AC%E4%B9%89/&text=Newtonsoft.Json高级用法之枚举中文转义Newtonsoft.Json高级用法之枚举中文转义" target="_blank" rel="noopener"><i class="fab fa-twitter " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://www.linkedin.com/shareArticle?url=http://yoursite.com/2019/01/06/Newtonsoft.Json%E9%AB%98%E7%BA%A7%E7%94%A8%E6%B3%95%E4%B9%8B%E6%9E%9A%E4%B8%BE%E4%B8%AD%E6%96%87%E8%BD%AC%E4%B9%89Newtonsoft.Json%E9%AB%98%E7%BA%A7%E7%94%A8%E6%B3%95%E4%B9%8B%E6%9E%9A%E4%B8%BE%E4%B8%AD%E6%96%87%E8%BD%AC%E4%B9%89/&title=Newtonsoft.Json高级用法之枚举中文转义Newtonsoft.Json高级用法之枚举中文转义" target="_blank" rel="noopener"><i class="fab fa-linkedin " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://pinterest.com/pin/create/bookmarklet/?url=http://yoursite.com/2019/01/06/Newtonsoft.Json%E9%AB%98%E7%BA%A7%E7%94%A8%E6%B3%95%E4%B9%8B%E6%9E%9A%E4%B8%BE%E4%B8%AD%E6%96%87%E8%BD%AC%E4%B9%89Newtonsoft.Json%E9%AB%98%E7%BA%A7%E7%94%A8%E6%B3%95%E4%B9%8B%E6%9E%9A%E4%B8%BE%E4%B8%AD%E6%96%87%E8%BD%AC%E4%B9%89/&is_video=false&description=Newtonsoft.Json高级用法之枚举中文转义Newtonsoft.Json高级用法之枚举中文转义" target="_blank" rel="noopener"><i class="fab fa-pinterest " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="mailto:?subject=Newtonsoft.Json高级用法之枚举中文转义Newtonsoft.Json高级用法之枚举中文转义&body=Check out this article: http://yoursite.com/2019/01/06/Newtonsoft.Json%E9%AB%98%E7%BA%A7%E7%94%A8%E6%B3%95%E4%B9%8B%E6%9E%9A%E4%B8%BE%E4%B8%AD%E6%96%87%E8%BD%AC%E4%B9%89Newtonsoft.Json%E9%AB%98%E7%BA%A7%E7%94%A8%E6%B3%95%E4%B9%8B%E6%9E%9A%E4%B8%BE%E4%B8%AD%E6%96%87%E8%BD%AC%E4%B9%89/"><i class="fas fa-envelope " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://getpocket.com/save?url=http://yoursite.com/2019/01/06/Newtonsoft.Json%E9%AB%98%E7%BA%A7%E7%94%A8%E6%B3%95%E4%B9%8B%E6%9E%9A%E4%B8%BE%E4%B8%AD%E6%96%87%E8%BD%AC%E4%B9%89Newtonsoft.Json%E9%AB%98%E7%BA%A7%E7%94%A8%E6%B3%95%E4%B9%8B%E6%9E%9A%E4%B8%BE%E4%B8%AD%E6%96%87%E8%BD%AC%E4%B9%89/&title=Newtonsoft.Json高级用法之枚举中文转义Newtonsoft.Json高级用法之枚举中文转义" target="_blank" rel="noopener"><i class="fab fa-get-pocket " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://reddit.com/submit?url=http://yoursite.com/2019/01/06/Newtonsoft.Json%E9%AB%98%E7%BA%A7%E7%94%A8%E6%B3%95%E4%B9%8B%E6%9E%9A%E4%B8%BE%E4%B8%AD%E6%96%87%E8%BD%AC%E4%B9%89Newtonsoft.Json%E9%AB%98%E7%BA%A7%E7%94%A8%E6%B3%95%E4%B9%8B%E6%9E%9A%E4%B8%BE%E4%B8%AD%E6%96%87%E8%BD%AC%E4%B9%89/&title=Newtonsoft.Json高级用法之枚举中文转义Newtonsoft.Json高级用法之枚举中文转义" target="_blank" rel="noopener"><i class="fab fa-reddit " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://www.stumbleupon.com/submit?url=http://yoursite.com/2019/01/06/Newtonsoft.Json%E9%AB%98%E7%BA%A7%E7%94%A8%E6%B3%95%E4%B9%8B%E6%9E%9A%E4%B8%BE%E4%B8%AD%E6%96%87%E8%BD%AC%E4%B9%89Newtonsoft.Json%E9%AB%98%E7%BA%A7%E7%94%A8%E6%B3%95%E4%B9%8B%E6%9E%9A%E4%B8%BE%E4%B8%AD%E6%96%87%E8%BD%AC%E4%B9%89/&title=Newtonsoft.Json高级用法之枚举中文转义Newtonsoft.Json高级用法之枚举中文转义" target="_blank" rel="noopener"><i class="fab fa-stumbleupon " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://digg.com/submit?url=http://yoursite.com/2019/01/06/Newtonsoft.Json%E9%AB%98%E7%BA%A7%E7%94%A8%E6%B3%95%E4%B9%8B%E6%9E%9A%E4%B8%BE%E4%B8%AD%E6%96%87%E8%BD%AC%E4%B9%89Newtonsoft.Json%E9%AB%98%E7%BA%A7%E7%94%A8%E6%B3%95%E4%B9%8B%E6%9E%9A%E4%B8%BE%E4%B8%AD%E6%96%87%E8%BD%AC%E4%B9%89/&title=Newtonsoft.Json高级用法之枚举中文转义Newtonsoft.Json高级用法之枚举中文转义" target="_blank" rel="noopener"><i class="fab fa-digg " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://www.tumblr.com/share/link?url=http://yoursite.com/2019/01/06/Newtonsoft.Json%E9%AB%98%E7%BA%A7%E7%94%A8%E6%B3%95%E4%B9%8B%E6%9E%9A%E4%B8%BE%E4%B8%AD%E6%96%87%E8%BD%AC%E4%B9%89Newtonsoft.Json%E9%AB%98%E7%BA%A7%E7%94%A8%E6%B3%95%E4%B9%8B%E6%9E%9A%E4%B8%BE%E4%B8%AD%E6%96%87%E8%BD%AC%E4%B9%89/&name=Newtonsoft.Json高级用法之枚举中文转义Newtonsoft.Json高级用法之枚举中文转义&description=" target="_blank" rel="noopener"><i class="fab fa-tumblr " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://news.ycombinator.com/submitlink?u=http://yoursite.com/2019/01/06/Newtonsoft.Json%E9%AB%98%E7%BA%A7%E7%94%A8%E6%B3%95%E4%B9%8B%E6%9E%9A%E4%B8%BE%E4%B8%AD%E6%96%87%E8%BD%AC%E4%B9%89Newtonsoft.Json%E9%AB%98%E7%BA%A7%E7%94%A8%E6%B3%95%E4%B9%8B%E6%9E%9A%E4%B8%BE%E4%B8%AD%E6%96%87%E8%BD%AC%E4%B9%89/&t=Newtonsoft.Json高级用法之枚举中文转义Newtonsoft.Json高级用法之枚举中文转义" target="_blank" rel="noopener"><i class="fab fa-hacker-news " aria-hidden="true"></i></a></li>
</ul>

    </div>
    <div id="toc">
      <ol class="toc"><li class="toc-item toc-level-4"><a class="toc-link" href="#Newtonsoft-Json高级用法"><span class="toc-number">1.</span> <span class="toc-text">Newtonsoft.Json高级用法</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#再谈Newtonsoft-Json高级用法"><span class="toc-number">2.</span> <span class="toc-text">再谈Newtonsoft.Json高级用法</span></a></li></ol></li></ol></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#枚举值序列化问题（摘自焰尾迭-段落）"><span class="toc-number"></span> <span class="toc-text">枚举值序列化问题（摘自焰尾迭 段落）</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#思考"><span class="toc-number"></span> <span class="toc-text">思考</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#枚举值中文序列化"><span class="toc-number"></span> <span class="toc-text">枚举值中文序列化</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#总结"><span class="toc-number"></span> <span class="toc-text">总结</span></a>
    </div>
  </span>
</div>

    
    <div class="content index py4">
        
        <article class="post" itemscope itemtype="http://schema.org/BlogPosting">
  <header>
    
    <h1 class="posttitle" itemprop="name headline">
        Newtonsoft.Json高级用法之枚举中文转义Newtonsoft.Json高级用法之枚举中文转义
    </h1>



    <div class="meta">
      <span class="author" itemprop="author" itemscope itemtype="http://schema.org/Person">
        <span itemprop="name">TmoonSite</span>
      </span>
      
    <div class="postdate">
      
        <time datetime="2019-01-05T23:13:35.000Z" itemprop="datePublished">2019-01-06</time>
        
      
    </div>


      

      

    </div>
  </header>
  

  <div class="content" itemprop="articleBody">
    <p> 最近看博客园中 <a href="http://home.cnblogs.com/u/yanweidie/" target="_blank" rel="noopener">焰尾迭</a><a href="http://home.cnblogs.com/u/yanweidie/" target="_blank" rel="noopener"> </a>的两篇关于”Newtonsoft.Json高级用法”的文章受到了很多人的评论，一度登入到头条推荐。</p>
<p>今天我就不再重复<a href="http://home.cnblogs.com/u/yanweidie/" target="_blank" rel="noopener">焰尾迭</a>博文中的一些提过的Newtonsoft.Json的高级用法。大家如果想知道直接去看。</p>
<h4 id="Newtonsoft-Json高级用法"><a href="#Newtonsoft-Json高级用法" class="headerlink" title="Newtonsoft.Json高级用法"></a><a href="http://www.cnblogs.com/yanweidie/p/4605212.html" target="_blank" rel="noopener">Newtonsoft.Json高级用法</a></h4><h4 id="再谈Newtonsoft-Json高级用法"><a href="#再谈Newtonsoft-Json高级用法" class="headerlink" title="再谈Newtonsoft.Json高级用法"></a><a href="http://www.cnblogs.com/yanweidie/p/5155268.html" target="_blank" rel="noopener">再谈Newtonsoft.Json高级用法</a></h4><p>我主要说<a href="http://home.cnblogs.com/u/yanweidie/" target="_blank" rel="noopener">焰尾迭</a><a href="http://home.cnblogs.com/u/yanweidie/" target="_blank" rel="noopener"> </a>没有提到的用法——枚举中文转义</p>
<h1 id="枚举值序列化问题（摘自焰尾迭-段落）"><a href="#枚举值序列化问题（摘自焰尾迭-段落）" class="headerlink" title="枚举值序列化问题（摘自焰尾迭 段落）"></a>枚举值序列化问题（摘自<a href="http://home.cnblogs.com/u/yanweidie/" target="_blank" rel="noopener">焰尾迭</a><a href="http://home.cnblogs.com/u/yanweidie/" target="_blank" rel="noopener"> </a>段落）</h1><p>public enum NotifyType</p>
<p>{</p>
<p>/// <summary></p>
<p>/// Emil发送</p>
<p>/// </summary></p>
<p>Mail=0,</p>
<p>/// <summary></p>
<p>/// 短信发送</p>
<p>/// </summary></p>
<p>SMS=1</p>
<p>}</p>
<p>public class TestEnmu</p>
<p>{</p>
<p>/// <summary></p>
<p>/// 消息发送类型</p>
<p>/// </summary></p>
<p>public NotifyType Type { get; set; }</p>
<p>}</p>
<p>JsonConvert.SerializeObject(new TestEnmu());</p>
<p>输出结果：<img src="https://images2015.cnblogs.com/blog/336693/201509/336693-20150910171743028-388882267.png" alt="">  现在改造一下，输出”Type”:”Mail”</p>
<p><img src="https://common.cnblogs.com/images/copycode.gif" alt=""></p>
<p><img src="https://common.cnblogs.com/images/copycode.gif" alt=""></p>
<p>public class TestEnmu</p>
<p>{</p>
<p>/// <summary></p>
<p>/// 消息发送类型</p>
<p>/// </summary></p>
<p>[JsonConverter(typeof(StringEnumConverter))]</p>
<p>public NotifyType Type { get; set; }</p>
<p>}</p>
<p><img src="https://common.cnblogs.com/images/copycode.gif" alt=""></p>
<p><img src="https://common.cnblogs.com/images/copycode.gif" alt=""></p>
<p>其它的都不变，在Type属性上加上了JsonConverter(typeof(StringEnumConverter))表示将枚举值转换成对应的字符串,而StringEnumConverter是Newtonsoft.Json内置的转换类型,最终输出结果</p>
<p><img src="https://images2015.cnblogs.com/blog/336693/201509/336693-20150910172109044-1714459886.png" alt=""></p>
<h1 id="思考"><a href="#思考" class="headerlink" title="思考"></a>思考</h1><p>到这里StringEnumConverter确实为我们解决了很多问题，从枚举值类型转到名称。 如果我们是英国人该多好呀，到这里Newtonsoft.Json 自带的Converter已经为我们解决一切问题。</p>
<p>谁叫我们是中国人，到到枚举转成Mail还是很不爽。</p>
<p>如果能把枚举转成”电子邮箱“才符合我们中国人的习惯。</p>
<p>{</p>
<p>“Type”:”电子邮箱”</p>
<p>}</p>
<h1 id="枚举值中文序列化"><a href="#枚举值中文序列化" class="headerlink" title="枚举值中文序列化"></a>枚举值中文序列化</h1><p>首先根据上面的目标我们首先改造下我们的枚举类型</p>
<p><img src="https://common.cnblogs.com/images/copycode.gif" alt=""></p>
<p>public enum NotifyType</p>
<p>{</p>
<p>/// <summary></p>
<p>/// 电子邮箱</p>
<p>/// </summary></p>
<p>[Description(“电子邮箱”)]</p>
<p>Mail = 0,</p>
<p>/// <summary></p>
<p>/// 手机短信</p>
<p>/// </summary></p>
<p>[Description(“手机短信”)]</p>
<p>SMS = 1</p>
<p>}</p>
<p><img src="https://common.cnblogs.com/images/copycode.gif" alt=""></p>
<p>接下来我们自定义一个EnumJsonConvert 类，考虑到我们枚举可能会传入不同类型，我们这里使用泛型</p>
<p><img src="https://common.cnblogs.com/images/copycode.gif" alt=""></p>
<p>public class EnumJsonConvert<T>:JsonConverter where T: struct, IConvertible</p>
<p>{</p>
<p>public void EnumConverter()</p>
<p>{</p>
<p>if (!typeof (T).IsEnum)</p>
<p>{</p>
<p>throw new ArgumentException(“T 必须是枚举类型”);</p>
<p>}</p>
<p>}</p>
<p>public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)</p>
<p>{</p>
<p>if (reader.TokenType == JsonToken.Null)</p>
<p>{</p>
<p>return null;</p>
<p>}</p>
<p>try</p>
<p>{</p>
<p>return reader.Value.ToString();</p>
<p>}</p>
<p>catch (Exception ex)</p>
<p>{</p>
<p>throw new Exception(string.Format(“不能将枚举{1}的值{0}转换为Json格式.”, reader.Value, objectType));</p>
<p>}</p>
<p>}</p>
<p>/// <summary></p>
<p>/// 判断是否为Bool类型</p>
<p>/// </summary></p>
<p>/// <param name="objectType">类型</param></p>
<p>/// <returns>为bool类型则可以进行转换</returns></p>
<p>public override bool CanConvert(Type objectType)</p>
<p>{</p>
<p>return true;</p>
<p>}</p>
<p>public bool IsNullableType(Type t)</p>
<p>{</p>
<p>if (t == null)</p>
<p>{</p>
<p>throw new ArgumentNullException(nameof(t));</p>
<p>}</p>
<p>return t.BaseType != null &amp;&amp; (t.BaseType.FullName == “System.ValueType” &amp;&amp; t.GetGenericTypeDefinition() == typeof(Nullable&lt;&gt;));</p>
<p>}</p>
<p>public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)</p>
<p>{</p>
<p>if (value == null)</p>
<p>{</p>
<p>writer.WriteNull();</p>
<p>return;</p>
<p>}</p>
<p>string bValue = value.ToString();</p>
<p>int isNo;</p>
<p>if (int.TryParse(bValue, out isNo))</p>
<p>{</p>
<p>bValue= GetEnumDescription(typeof(T), isNo);</p>
<p>}</p>
<p>else</p>
<p>{</p>
<p>bValue= GetEnumDescription(typeof(T), value.ToString());</p>
<p>}</p>
<p>writer.WriteValue(bValue);</p>
<p>}</p>
<p>/// <summary></p>
<p>/// 获取枚举描述</p>
<p>/// </summary></p>
<p>/// <param name="type">枚举类型</param></p>
<p>/// <param name="value">枚举名称</param></p>
<p>/// <returns></returns></p>
<p>private string GetEnumDescription(Type type, string value)</p>
<p>{</p>
<p>try</p>
<p>{</p>
<p>FieldInfo field = type.GetField(value);</p>
<p>if (field == null)</p>
<p>{</p>
<p>return “”;</p>
<p>}</p>
<p>var desc = Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute)) as DescriptionAttribute;</p>
<p>if (desc != null) return desc.Description;</p>
<p>return “”;</p>
<p>}</p>
<p>catch</p>
<p>{</p>
<p>return “”;</p>
<p>}</p>
<p>}</p>
<p>/// <summary></p>
<p>/// 获取枚举描述</p>
<p>/// </summary></p>
<p>/// <param name="type">枚举类型</param></p>
<p>/// <param name="value">枚举hasecode</param></p>
<p>/// <returns></returns></p>
<p>private string GetEnumDescription(Type type, int value)</p>
<p>{</p>
<p>try</p>
<p>{</p>
<p>FieldInfo field = type.GetField(Enum.GetName(type, value));</p>
<p>if (field == null)</p>
<p>{</p>
<p>return “”;</p>
<p>}</p>
<p>var desc = Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute)) as DescriptionAttribute;</p>
<p>if (desc != null) return desc.Description;</p>
<p>return “”;</p>
<p>}</p>
<p>catch</p>
<p>{</p>
<p>return “”;</p>
<p>}</p>
<p>}</p>
<p>}</p>
<p><img src="https://common.cnblogs.com/images/copycode.gif" alt=""></p>
<p>到这里我们自定义的JsonConvert已经实现好了。开始使用吧。</p>
<p>我们定义一个类Myclass</p>
<p><img src="https://common.cnblogs.com/images/copycode.gif" alt=""></p>
<p>public class MyClass</p>
<p>{</p>
<p>public string UserName { get; set; }</p>
<p>[JsonConverter(typeof(EnumJsonConvert<NotifyType>))] // 这里就是 我们特殊的地方</p>
<p>public int NotifyType { get; set; }</p>
<p>}</p>
<p><img src="https://common.cnblogs.com/images/copycode.gif" alt=""></p>
<p>我们写个测试看对不对：</p>
<p><img src="" alt=""></p>
<p>结果如下：</p>
<p><img src="" alt=""></p>
<p>我们实现了最初的目标，一般我们数据库到保存着枚举的值类型，比如这里的 0，1，2，3……。 我们前端一般需要显示我们的语言，比如这里的电子邮箱，手机短信……</p>
<h1 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h1><p>其实这里自定义实现了一个json自定义类型转换。利用枚举的Description特性利用反射实现枚举中文转义。其实这类转义在我们平时开发过程中经常见到。用JsonConvert可以给我们带来一定的解耦。</p>
<p>省去if esleif 等判断。</p>
<p>从上到下，主要只讲解了一个问题，字里行间语言上都有很多的不严谨。不过知识点涉及到Newtonsoft.Json的自定义类型转换，反射等。代码全部贴上，希望对大家有所帮助。谢谢。</p>
<p>再次感谢<a href="http://home.cnblogs.com/u/yanweidie/" target="_blank" rel="noopener">焰尾迭</a>的分享。 谢谢！，同时也希望也能和你一样被大家推荐。谢谢。</p>

  </div>
</article>



        
          <div id="footer-post-container">
  <div id="footer-post">

    <div id="nav-footer" style="display: none">
      <ul>
         
          <li><a href="/">Home</a></li>
         
          <li><a href="/about/">About</a></li>
         
          <li><a href="/archives/">Writing</a></li>
         
          <li><a href="/projects_url">Projects</a></li>
        
      </ul>
    </div>

    <div id="toc-footer" style="display: none">
      <ol class="toc"><li class="toc-item toc-level-4"><a class="toc-link" href="#Newtonsoft-Json高级用法"><span class="toc-number">1.</span> <span class="toc-text">Newtonsoft.Json高级用法</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#再谈Newtonsoft-Json高级用法"><span class="toc-number">2.</span> <span class="toc-text">再谈Newtonsoft.Json高级用法</span></a></li></ol></li></ol></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#枚举值序列化问题（摘自焰尾迭-段落）"><span class="toc-number"></span> <span class="toc-text">枚举值序列化问题（摘自焰尾迭 段落）</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#思考"><span class="toc-number"></span> <span class="toc-text">思考</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#枚举值中文序列化"><span class="toc-number"></span> <span class="toc-text">枚举值中文序列化</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#总结"><span class="toc-number"></span> <span class="toc-text">总结</span></a>
    </div>

    <div id="share-footer" style="display: none">
      <ul>
  <li><a class="icon" href="http://www.facebook.com/sharer.php?u=http://yoursite.com/2019/01/06/Newtonsoft.Json%E9%AB%98%E7%BA%A7%E7%94%A8%E6%B3%95%E4%B9%8B%E6%9E%9A%E4%B8%BE%E4%B8%AD%E6%96%87%E8%BD%AC%E4%B9%89Newtonsoft.Json%E9%AB%98%E7%BA%A7%E7%94%A8%E6%B3%95%E4%B9%8B%E6%9E%9A%E4%B8%BE%E4%B8%AD%E6%96%87%E8%BD%AC%E4%B9%89/" target="_blank" rel="noopener"><i class="fab fa-facebook fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://twitter.com/share?url=http://yoursite.com/2019/01/06/Newtonsoft.Json%E9%AB%98%E7%BA%A7%E7%94%A8%E6%B3%95%E4%B9%8B%E6%9E%9A%E4%B8%BE%E4%B8%AD%E6%96%87%E8%BD%AC%E4%B9%89Newtonsoft.Json%E9%AB%98%E7%BA%A7%E7%94%A8%E6%B3%95%E4%B9%8B%E6%9E%9A%E4%B8%BE%E4%B8%AD%E6%96%87%E8%BD%AC%E4%B9%89/&text=Newtonsoft.Json高级用法之枚举中文转义Newtonsoft.Json高级用法之枚举中文转义" target="_blank" rel="noopener"><i class="fab fa-twitter fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://www.linkedin.com/shareArticle?url=http://yoursite.com/2019/01/06/Newtonsoft.Json%E9%AB%98%E7%BA%A7%E7%94%A8%E6%B3%95%E4%B9%8B%E6%9E%9A%E4%B8%BE%E4%B8%AD%E6%96%87%E8%BD%AC%E4%B9%89Newtonsoft.Json%E9%AB%98%E7%BA%A7%E7%94%A8%E6%B3%95%E4%B9%8B%E6%9E%9A%E4%B8%BE%E4%B8%AD%E6%96%87%E8%BD%AC%E4%B9%89/&title=Newtonsoft.Json高级用法之枚举中文转义Newtonsoft.Json高级用法之枚举中文转义" target="_blank" rel="noopener"><i class="fab fa-linkedin fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://pinterest.com/pin/create/bookmarklet/?url=http://yoursite.com/2019/01/06/Newtonsoft.Json%E9%AB%98%E7%BA%A7%E7%94%A8%E6%B3%95%E4%B9%8B%E6%9E%9A%E4%B8%BE%E4%B8%AD%E6%96%87%E8%BD%AC%E4%B9%89Newtonsoft.Json%E9%AB%98%E7%BA%A7%E7%94%A8%E6%B3%95%E4%B9%8B%E6%9E%9A%E4%B8%BE%E4%B8%AD%E6%96%87%E8%BD%AC%E4%B9%89/&is_video=false&description=Newtonsoft.Json高级用法之枚举中文转义Newtonsoft.Json高级用法之枚举中文转义" target="_blank" rel="noopener"><i class="fab fa-pinterest fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="mailto:?subject=Newtonsoft.Json高级用法之枚举中文转义Newtonsoft.Json高级用法之枚举中文转义&body=Check out this article: http://yoursite.com/2019/01/06/Newtonsoft.Json%E9%AB%98%E7%BA%A7%E7%94%A8%E6%B3%95%E4%B9%8B%E6%9E%9A%E4%B8%BE%E4%B8%AD%E6%96%87%E8%BD%AC%E4%B9%89Newtonsoft.Json%E9%AB%98%E7%BA%A7%E7%94%A8%E6%B3%95%E4%B9%8B%E6%9E%9A%E4%B8%BE%E4%B8%AD%E6%96%87%E8%BD%AC%E4%B9%89/"><i class="fas fa-envelope fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://getpocket.com/save?url=http://yoursite.com/2019/01/06/Newtonsoft.Json%E9%AB%98%E7%BA%A7%E7%94%A8%E6%B3%95%E4%B9%8B%E6%9E%9A%E4%B8%BE%E4%B8%AD%E6%96%87%E8%BD%AC%E4%B9%89Newtonsoft.Json%E9%AB%98%E7%BA%A7%E7%94%A8%E6%B3%95%E4%B9%8B%E6%9E%9A%E4%B8%BE%E4%B8%AD%E6%96%87%E8%BD%AC%E4%B9%89/&title=Newtonsoft.Json高级用法之枚举中文转义Newtonsoft.Json高级用法之枚举中文转义" target="_blank" rel="noopener"><i class="fab fa-get-pocket fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://reddit.com/submit?url=http://yoursite.com/2019/01/06/Newtonsoft.Json%E9%AB%98%E7%BA%A7%E7%94%A8%E6%B3%95%E4%B9%8B%E6%9E%9A%E4%B8%BE%E4%B8%AD%E6%96%87%E8%BD%AC%E4%B9%89Newtonsoft.Json%E9%AB%98%E7%BA%A7%E7%94%A8%E6%B3%95%E4%B9%8B%E6%9E%9A%E4%B8%BE%E4%B8%AD%E6%96%87%E8%BD%AC%E4%B9%89/&title=Newtonsoft.Json高级用法之枚举中文转义Newtonsoft.Json高级用法之枚举中文转义" target="_blank" rel="noopener"><i class="fab fa-reddit fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://www.stumbleupon.com/submit?url=http://yoursite.com/2019/01/06/Newtonsoft.Json%E9%AB%98%E7%BA%A7%E7%94%A8%E6%B3%95%E4%B9%8B%E6%9E%9A%E4%B8%BE%E4%B8%AD%E6%96%87%E8%BD%AC%E4%B9%89Newtonsoft.Json%E9%AB%98%E7%BA%A7%E7%94%A8%E6%B3%95%E4%B9%8B%E6%9E%9A%E4%B8%BE%E4%B8%AD%E6%96%87%E8%BD%AC%E4%B9%89/&title=Newtonsoft.Json高级用法之枚举中文转义Newtonsoft.Json高级用法之枚举中文转义" target="_blank" rel="noopener"><i class="fab fa-stumbleupon fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://digg.com/submit?url=http://yoursite.com/2019/01/06/Newtonsoft.Json%E9%AB%98%E7%BA%A7%E7%94%A8%E6%B3%95%E4%B9%8B%E6%9E%9A%E4%B8%BE%E4%B8%AD%E6%96%87%E8%BD%AC%E4%B9%89Newtonsoft.Json%E9%AB%98%E7%BA%A7%E7%94%A8%E6%B3%95%E4%B9%8B%E6%9E%9A%E4%B8%BE%E4%B8%AD%E6%96%87%E8%BD%AC%E4%B9%89/&title=Newtonsoft.Json高级用法之枚举中文转义Newtonsoft.Json高级用法之枚举中文转义" target="_blank" rel="noopener"><i class="fab fa-digg fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://www.tumblr.com/share/link?url=http://yoursite.com/2019/01/06/Newtonsoft.Json%E9%AB%98%E7%BA%A7%E7%94%A8%E6%B3%95%E4%B9%8B%E6%9E%9A%E4%B8%BE%E4%B8%AD%E6%96%87%E8%BD%AC%E4%B9%89Newtonsoft.Json%E9%AB%98%E7%BA%A7%E7%94%A8%E6%B3%95%E4%B9%8B%E6%9E%9A%E4%B8%BE%E4%B8%AD%E6%96%87%E8%BD%AC%E4%B9%89/&name=Newtonsoft.Json高级用法之枚举中文转义Newtonsoft.Json高级用法之枚举中文转义&description=" target="_blank" rel="noopener"><i class="fab fa-tumblr fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://news.ycombinator.com/submitlink?u=http://yoursite.com/2019/01/06/Newtonsoft.Json%E9%AB%98%E7%BA%A7%E7%94%A8%E6%B3%95%E4%B9%8B%E6%9E%9A%E4%B8%BE%E4%B8%AD%E6%96%87%E8%BD%AC%E4%B9%89Newtonsoft.Json%E9%AB%98%E7%BA%A7%E7%94%A8%E6%B3%95%E4%B9%8B%E6%9E%9A%E4%B8%BE%E4%B8%AD%E6%96%87%E8%BD%AC%E4%B9%89/&t=Newtonsoft.Json高级用法之枚举中文转义Newtonsoft.Json高级用法之枚举中文转义" target="_blank" rel="noopener"><i class="fab fa-hacker-news fa-lg" aria-hidden="true"></i></a></li>
</ul>

    </div>

    <div id="actions-footer">
        <a id="menu" class="icon" href="#" onclick="$('#nav-footer').toggle();return false;"><i class="fas fa-bars fa-lg" aria-hidden="true"></i> Menu</a>
        <a id="toc" class="icon" href="#" onclick="$('#toc-footer').toggle();return false;"><i class="fas fa-list fa-lg" aria-hidden="true"></i> TOC</a>
        <a id="share" class="icon" href="#" onclick="$('#share-footer').toggle();return false;"><i class="fas fa-share-alt fa-lg" aria-hidden="true"></i> Share</a>
        <a id="top" style="display:none" class="icon" href="#" onclick="$('html, body').animate({ scrollTop: 0 }, 'fast');"><i class="fas fa-chevron-up fa-lg" aria-hidden="true"></i> Top</a>
    </div>

  </div>
</div>

        
        <footer id="footer">
  <div class="footer-left">
    Copyright &copy; 2020 Tmoonlight
  </div>
  <div class="footer-right">
    <nav>
      <ul>
         
          <li><a href="/">Home</a></li>
         
          <li><a href="/about/">About</a></li>
         
          <li><a href="/archives/">Writing</a></li>
         
          <li><a href="/projects_url">Projects</a></li>
        
      </ul>
    </nav>
  </div>
</footer>

    </div>
    <!-- styles -->

<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">


<link rel="stylesheet" href="/lib/justified-gallery/css/justifiedGallery.min.css">


    <!-- jquery -->

<script src="/lib/jquery/jquery.min.js"></script>


<script src="/lib/justified-gallery/js/jquery.justifiedGallery.min.js"></script>

<!-- clipboard -->

  
<script src="/lib/clipboard/clipboard.min.js"></script>

  <script type="text/javascript">
  $(function() {
    // copy-btn HTML
    var btn = "<span class=\"btn-copy tooltipped tooltipped-sw\" aria-label=\"Copy to clipboard!\">";
    btn += '<i class="far fa-clone"></i>';
    btn += '</span>'; 
    // mount it!
    $(".highlight table").before(btn);
    var clip = new ClipboardJS('.btn-copy', {
      text: function(trigger) {
        return Array.from(trigger.nextElementSibling.querySelectorAll('.code')).reduce((str,it)=>str+it.innerText+'\n','')
      }
    });
    clip.on('success', function(e) {
      e.trigger.setAttribute('aria-label', "Copied!");
      e.clearSelection();
    })
  })
  </script>


<script src="/js/main.js"></script>

<!-- search -->

<!-- Google Analytics -->

<!-- Baidu Analytics -->

<!-- Disqus Comments -->


</body>
</html>
